
import PointRoom from "../../Entities/PointRoom";
import PoolRoom from "../../Entities/PoolRoom";
import DealRoom from "../../Entities/DealRoom";
import UserSeat from "./UserSeat"

cc.Class({
    extends: UserSeat,

    properties: {
        placeHolderNode:{
            default:null,
            type:cc.Node
        },

        seatNode:{
            default:null,
            type:cc.Node
        },

        faceSprite:{
            default:null,
            type:cc.Sprite
        },

        labelName:{
            default:null,
            type:cc.Label
        },

        ptsNode:{
            default:null,
            type:cc.Node
        },

        labelPTSScore:{
            default:null,
            type:cc.Label
        },

        progressBar:{
            default:null,
            type:cc.ProgressBar
        },

        countDownNode:{
            default:null,
            type:cc.Node
        },

        labelTotalTime:{
            default:null,
            type:cc.Label
        },

        labelNowTime:{
            default:null,
            type:cc.Label
        },

        iconReady:{
            default:null,
            type:cc.Node
        },

        netNode:{
            default:null,
            type:cc.Node
        },

        headBg:{
            default:null,
            type:cc.Node
        },

        faceMask:{
            default:null,
            type:cc.Node,
        },

        dropMask:{
            default:null,
            type:cc.Node
        }
    },

    changeFaceBg:function (colorString) {
        this.headBg.color = (new cc.color()).fromHEX("#"+colorString)
    },

    changeNet:function (isGood) {
        if(isGood){
            cc.find("Good",this.netNode).active = true
            cc.find("Off",this.netNode).active = false
        }else {
            cc.find("Good",this.netNode).active = false
            cc.find("Off",this.netNode).active = true
        }
    },

    onLoad () {
        this._super()
        this._setFaceWhite()
        this.iconReady.active = false
        if(this.dropMask) this.dropMask.active = false
        cc.find("Good",this.netNode).active = true
        cc.find("Off",this.netNode).active = false

        // this.updateGameScore(0)
    },

    initSeat:function (userObj) {
        cc.log("初始化座位：",userObj)
        this.playerInfo = userObj
        if(!userObj){
            this.placeHolderNode.opacity = 255
            this.placeHolderNode.y = 0
            this.placeHolderNode.active = true
            this.seatNode.active = false
        }else {
            //直接初始化座位
            this.seatNode.y = 0
            this.placeHolderNode.active = false
            this.seatNode.opacity = 255
            this.seatNode.active = true
            this.initTPPHead(userObj)
        }
    },

    enterUser:function (userObj) {
        this.initTPPHead(userObj)

        let disHeight = 30

        this.placeHolderNode.stopAllActions()
        this.seatNode.stopAllActions()
        this.unschedule(this.delaySchedule);
        this.placeHolderNode.active = true
        this.placeHolderNode.y = 0
        this.placeHolderNode.opacity = 255
        let moveUp = cc.moveTo(0.2,cc.v2(0,disHeight))
        let fadeOut = cc.fadeOut(0.3)
        let spawn = cc.spawn(moveUp,fadeOut)
        let callfunc = cc.callFunc(function () {
            this.placeHolderNode.active = false
        }.bind(this),this)
        let seq = cc.sequence(spawn,callfunc)
        this.placeHolderNode.runAction(seq)

        this.seatNode.active = false
        this.delaySchedule = function () {
            this.seatNode.stopAllActions()
            this.seatNode.y = disHeight
            this.seatNode.active = true
            this.seatNode.opacity = 0
            let fadeIn = cc.fadeIn(0.2)
            let moveDown = cc.moveBy(0.2,cc.v2(0,-disHeight))
            let spawn = cc.spawn(fadeIn,moveDown)
            let callFuncDown = cc.callFunc(function () {
                this.seatNode.y = 0
            }.bind(this),this)
            let seqDown = cc.sequence(spawn,callFuncDown)
            this.seatNode.runAction(seqDown)
        }.bind(this)
        this.scheduleOnce(this.delaySchedule,0.2)
    },

    userOut:function (userObj) {
        this.clearDataOnly()
        let disHeight = 30

        this.placeHolderNode.stopAllActions()
        this.seatNode.stopAllActions()
        this.unschedule(this.delaySchedule);
        this.seatNode.active = true
        this.seatNode.y = 0
        this.seatNode.opacity = 255
        let moveUp = cc.moveTo(0.2,cc.v2(0,disHeight))
        let fadeOut = cc.fadeOut(0.3)
        let spawn = cc.spawn(moveUp,fadeOut)
        let callfunc = cc.callFunc(function () {
            this.showIndicatorFirstSeat(false)
            this.seatNode.active = false
        }.bind(this),this)
        let seq = cc.sequence(spawn,callfunc)
        this.seatNode.runAction(seq)

        this.placeHolderNode.active = false
        this.delaySchedule = function () {
            this.placeHolderNode.stopAllActions()
            this.placeHolderNode.y = disHeight
            this.placeHolderNode.active = true
            this.placeHolderNode.opacity = 0

            let fadeIn = cc.fadeIn(0.2)
            let moveDown = cc.moveBy(0.2,0,-disHeight)
            let spawn = cc.spawn(fadeIn,moveDown)
            let callFuncDown = cc.callFunc(function () {
                this.changeToEmpty()
            }.bind(this),this)
            let seqDown = cc.sequence(spawn,callFuncDown)
            this.placeHolderNode.runAction(seqDown)
        }.bind(this)
        this.scheduleOnce(this.delaySchedule,0.2)
    },



    _onUpdateBottomScore:function (playerId) {

        if(!this.playerInfo){
            cc.warn("TPPHead is not been initialized!")
            return
        }
        if(!this.labelHandScore)
            return

        if(this.playerInfo.id !== playerId) {
            cc.log("隐藏了一个labelHandScore")
            this.labelHandScore.node.active = false
            return
        }

        let seqCount = 0
        let hasPure = false
        for(let i=0;i<cc.gg.tpgMgr.pokerBottoms.length;i++){
            let bot = cc.gg.tpgMgr.pokerBottoms[i]
            if(!bot.type)
                continue

            if(bot.type.type === cc.gg.enumConfig.SeperateType.SEQUENCE){
                if(bot.type.pure) hasPure = true
                seqCount++
            }
        }

        let scoreTotal = 0
        for(let i=0;i<cc.gg.tpgMgr.pokerBottoms.length;i++){
            let bot = cc.gg.tpgMgr.pokerBottoms[i]
            if(!bot.type)
                continue

            if(seqCount > 1 && hasPure){
                if(bot.type.type === cc.gg.enumConfig.SeperateType.SEQUENCE) continue
                if(bot.type.type === cc.gg.enumConfig.SeperateType.SET) continue
            }

            scoreTotal += cc.gg.sortUtil.GET_CARDS_SUM(bot.pokers) || 0
        }

        this.labelHandScore.string = scoreTotal
    },

    start () {

    },

    update (dt) {
        // if(this._startCountting){
        //     this.changeTime(dt)
        // }
    },

    initTPPHead:function (userInfo) {
        this.playerInfo = userInfo

        if(userInfo.a_state === 0){
            this.statusLabel.string = ""
            this._setFaceWhite()
            this.changeNet(false)
        }else if(userInfo.a_state === 1){
            this.statusLabel.string = ""
            this._setFaceWhite()
            this.changeNet(true)
        }else if(userInfo.a_state === 2){
            this._setFaceGray()
            this.statusLabel.string = "Leave"
            this.changeNet(false)
        }

        this.initSeatState(userInfo.b_state)

        this.countDownNode.active = false

        this.labelName.string = userInfo.name
        if(cc.gg.dataMgr.room instanceof PointRoom){
            this.ptsNode.active = false
        }else if(cc.gg.dataMgr.room instanceof PoolRoom){
            this.ptsNode.active = true
            this.labelPTSScore.string = "PTS:0"
            this.updateGameScore(userInfo.score || 0)
        }else if(cc.gg.dataMgr.room instanceof DealRoom){
            this.ptsNode.active = true
            // if(userInfo.id !== cc.gg.dataMgr.getPlayerGameId()){
            //     this.ptsNode.x -= 130
            // }
            this.updateGameScore(userInfo.score || 0)
        }

        cc.gg.logicUtil.initHead(userInfo.img,this.faceSprite)
        this.changeFaceBg(userInfo.face_bg)
        // this.initFace(userInfo)
    },

    initSeatState:function (bstate) {
        cc.log("update opponents seat's b_state:",bstate)
        if(bstate === cc.gg.enumConfig.ReadyState.ELIMINATED){
            this.changeToEliminated()
        }else if(bstate === cc.gg.enumConfig.ReadyState.DROPED){
            this.changeToDrop()
        }else {
            this._setFaceWhite()
        }
    },

    startCountting:function (countSeconds) {
        this.countDownNode.active = true
        // this._noticePlayed = false
        this._startCountting = true
        this.progressBar.node.active = true
        this.progressBar.progress = 1

        this.totalSecond = countSeconds
        this.nowSecond = countSeconds

        this.labelTotalTime.string = this.nowSecond
        this.labelNowTime.string = this.nowSecond
    },

    stopCountting:function () {
        this.countDownNode.active = false
        // this._noticePlayed = false
        this._startCountting = false
        this.progressBar.node.active = false
    },

    changeToLeave:function () {
        this.statusLabel.string = "Leave"
        this._setFaceGray()
    },

    changeToEliminated:function () {
        this.statusLabel.string = "Eliminated"
        this._setFaceGray()
    },

    changeToDrop:function () {
        this.statusLabel.string = ""
        this._setFaceDrop()
    },


    changeToEmpty:function () {
        cc.log("清空座位属性！")
        // this.playerInfo = null

        this.statusLabel.string = ""
        this.labelName.string = ""
        this.labelPTSScore.string = "PTS:0"
        this.faceSprite.spriteFrame = null


        cc.find("Good",this.netNode).active = false
        cc.find("Off",this.netNode).active = false
        // this.netNode.active = false
        this._setFaceWhite()
    },

    clearDataOnly:function () {
        this.playerInfo = null
    },

    changeTime : function (dt) {
        this.nowSecond = dt
        if(this.nowSecond <= 0){
            this.nowSecond = 0
            return
        }

        // if(this.nowSecond < 5 && !this._noticePlayed){
        //     this._noticePlayed = true
            // cc.find("Canvas/SoundManager").getComponent("SoundManager").playTimeNoticeSound()
        // }

        this.progressBar.progress = this.nowSecond/this.totalSecond
        this.labelNowTime.string = parseInt(this.nowSecond)
    },
});
